package PKA;


import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.*;

/**
 * Created by IntelliJ IDEA.
 * User: prageeth
 * Date: 4/15/12
 * Time: 1:02 PM
 * To change this template use File | Settings | File Templates.
 */
public class SecureMsg
{

    public void sendMessage( String msg )
    {
        System.out.println( "Sending msg " + msg );
        byte[] sig = null;
        //get Key
        try
        {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance( "RSA" );
            KeyPair pair = keyPairGenerator.generateKeyPair();

            KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
            SecretKey secretKey = keyGenerator.generateKey();
            Cipher c4 = Cipher.getInstance( "DES" );
            c4.init(Cipher.ENCRYPT_MODE, secretKey );
            byte[] b = c4.doFinal( "abc".getBytes() );
            String s = new String(b);
            c4 = Cipher.getInstance( "DES" );
            c4.init( Cipher.DECRYPT_MODE, secretKey );
            b = c4.doFinal( s.getBytes() );
            s = new String(b, "UTF8");
            System.out.println( "s = " + s );

            KeyStore keyStore = KeyStore.getInstance("JCEKS" );

            keyStore.load(null, "".toCharArray());
            KeyStore.SecretKeyEntry entry = new KeyStore.SecretKeyEntry(secretKey);
//          keyStore.setEntry("a",entry, new KeyStore.PasswordProtection("abc".toCharArray()) );
            keyStore.setKeyEntry("A", secretKey, "a".toCharArray(), null );
            System.out.println("keyPair.getPrivate() = " + pair.getPrivate());
            System.out.println("keyPair.getPrivate() = " + pair.getPublic());

            Signature rsa = Signature.getInstance( "SHA1withRSA" );
            PrivateKey priv = pair.getPrivate();

            try
            {
                rsa.initSign( priv );
                rsa.update( msg.getBytes() );
                sig = rsa.sign();
                System.out.println( "sig = " + sig );
                rsa.initSign( priv );
            }
            catch( SignatureException e )
            {
                e.printStackTrace();
            }
            catch( InvalidKeyException e )
            {
                e.printStackTrace();
            }
            PublicKey pub = pair.getPublic();
            // encript
            System.out.println( "Encripted Msg " + msg );
            //send msg
            receiveMessage(msg, sig, pub);
            Cipher c1 = Cipher.getInstance("RSA");
            c1.init(Cipher.ENCRYPT_MODE, pub);
            byte[] cipherText = c1.doFinal(msg.getBytes());
            System.out.println(new String(cipherText));

            Cipher c2 = Cipher.getInstance("RSA");
            c1.init(Cipher.DECRYPT_MODE, pair.getPrivate());
            byte[] plainText = c1.doFinal(cipherText);
            System.out.println("msg = " + new String(plainText));

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void receiveMessage( String msg, byte[] sig, PublicKey pub )
    {
        System.out.println( "Encripted Msg " + msg );
        try
        {
            Signature rsa = Signature.getInstance( "SHA1withRSA" );
            rsa.initVerify( pub );
            /* Update anmd verify the data */
            rsa.update( msg.getBytes() );
            boolean verifies = rsa.verify( sig );
            System.out.println( "signature verifies: " + verifies );
        }
        catch( NoSuchAlgorithmException e )
        {
            e.printStackTrace();
        }
        catch( InvalidKeyException e )
        {
            e.printStackTrace();
        }
        catch( SignatureException e )
        {
            e.printStackTrace();
        }
        System.out.println( "Decripted Msg " + msg );
    }

    public static void main( String[] args )
    {
        SecureMsg secureMsg = new SecureMsg();
        secureMsg.sendMessage( "Prageeth" );
    }
}
